/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.miracleteam.miracle.dao.daoImpl;

import com.miracleteam.miracle.dao.YKienNhanXetDAO;
import com.miracleteam.miracle.entities.Ykiennhanxetgianhang;
import com.miracleteam.miracle.utils.HibernateUtil;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author DangKhoa
 */
public class YKienNhanXetDAOImpl implements YKienNhanXetDAO{
    Logger logger= Logger.getLogger(TinhThanhDAOImpl.class);
    public List LayYKienNhanXet(String MaGh) {
      
        List ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select yk from Ykiennhanxetgianhang yk where yk.daXoa=0 and yk.choPhepHienThi=1 and yk.gianhang.maGianHang =:maGH order by yk.thoiGianPost DESC";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maGH", MaGh);
            query.setFirstResult(0);
            query.setMaxResults(5);
           
            ds = query.list();
        } catch (Exception e) {
             logger.error(e.getMessage());
           
            System.out.println(e.getMessage());
        }
        return ds;
   
    }
     public int ThemDanhGia( Ykiennhanxetgianhang yk) {
         Session session = HibernateUtil.getSessionFactory().openSession();
         if(yk==null)
             return 0;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(yk);
           
            transaction.commit();
        } catch (HibernateException ex) {
            transaction.rollback();
            logger.error(ex.getMessage());
            System.err.println(ex);
        } finally {
            session.close();
        }
        return 1;
    }
         public String PhatSinhMaNhanXet()
    {
        String kq="NX-";
        Date date = new Date();
        
        kq+=String.valueOf(date.getDay());
        kq+=String.valueOf(date.getMonth());
        kq+=String.valueOf(date.getYear());
        kq+="-";
        kq+=String.valueOf(date.getHours());
        kq+=String.valueOf(date.getMinutes());
        kq+=String.valueOf(date.getSeconds());
        
        return kq;
    }
    
}
